package com.ruoyi.project.infoBaseProjectStatist.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.project.infoBaseProjectStatist.domain.InfoBaseProjectStatist;
import com.ruoyi.project.infoBaseProjectStatist.mapper.InfoBaseProjectStatistMapper;
import com.ruoyi.project.infoBaseProjectStatist.service.IInfoBaseProjectStatistService;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 项目数据统计Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-18
 */
@Service
public class InfoBaseProjectStatistServiceImpl implements IInfoBaseProjectStatistService {
    @Resource
    private InfoBaseProjectStatistMapper infoBaseProjectStatistMapper;

    @Resource
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询项目数据统计
     *
     * @param id 项目数据统计主键
     * @return 项目数据统计
     */
    @Override
    public InfoBaseProjectStatist selectInfoBaseProjectStatistById(Long id) {
        return infoBaseProjectStatistMapper.selectInfoBaseProjectStatistById(id);
    }

    /**
     * 查询项目数据统计列表
     *
     * @param infoBaseProjectStatist 项目数据统计
     * @return 项目数据统计
     */
    @Override
    public List<InfoBaseProjectStatist> selectInfoBaseProjectStatistList(InfoBaseProjectStatist infoBaseProjectStatist) {
        return infoBaseProjectStatistMapper.selectInfoBaseProjectStatistList(infoBaseProjectStatist);
    }

    /**
     * 新增项目数据统计
     *
     * @param infoBaseProjectStatist 项目数据统计
     * @return 结果
     */
    @Override
    public int insertInfoBaseProjectStatist(InfoBaseProjectStatist infoBaseProjectStatist) {
        return infoBaseProjectStatistMapper.insertInfoBaseProjectStatist(infoBaseProjectStatist);
    }

    /**
     * 修改项目数据统计
     *
     * @param infoBaseProjectStatist 项目数据统计
     * @return 结果
     */
    @Override
    public int updateInfoBaseProjectStatist(InfoBaseProjectStatist infoBaseProjectStatist) {
        return infoBaseProjectStatistMapper.updateInfoBaseProjectStatist(infoBaseProjectStatist);
    }

    /**
     * 批量删除项目数据统计
     *
     * @param ids 需要删除的项目数据统计主键
     * @return 结果
     */
    @Override
    public int deleteInfoBaseProjectStatistByIds(Long[] ids) {
        return infoBaseProjectStatistMapper.deleteInfoBaseProjectStatistByIds(ids);
    }

    /**
     * 删除项目数据统计信息
     *
     * @param id 项目数据统计主键
     * @return 结果
     */
    @Override
    public int deleteInfoBaseProjectStatistById(Long id) {
        return infoBaseProjectStatistMapper.deleteInfoBaseProjectStatistById(id);
    }

    @Override
    public List<InfoBaseProjectStatist> selectInfoBaseProjectStatistLists(Long deptId) {
        return infoBaseProjectStatistMapper.selectInfoBaseProjectStatistLists(deptId);
    }
    /**
     * 一级项目名称数据统计
     *
     * @param
     * @return List
     */
    @Override
    public List<Map<String,Object>> getProjectStatistics(JSONObject jsonObject){
        String startTime=null;
        String lastTime=null;
        String projectName=null;
        if(ObjectUtil.isNotEmpty(jsonObject.get("startTime"))){
            startTime=jsonObject.getString("startTime");
        }
        if(ObjectUtil.isNotEmpty(jsonObject.get("lastTime"))){
            lastTime=jsonObject.getString("lastTime");
        }
        if(ObjectUtil.isNotEmpty(jsonObject.get("projectName"))){
            projectName=jsonObject.getString("projectName");
        }
        return infoBaseProjectStatistMapper.selectProjectStatistics(startTime,lastTime,projectName);
    }

    /**
     * 二级项目名称数据统计
     *
     * @param
     * @return List
     */
    @Override
    public List<Map<String,Object>> getSecondProjectStatistics(Integer parentProjectId){
        return infoBaseProjectStatistMapper.selectSecondProjectStatistics(parentProjectId);
    }


    @Override
    public InfoBaseProjectStatist selectTotal() {
        return infoBaseProjectStatistMapper.selectTotal();
    }

    @Override
    public Map<String, Object> selectSs(Long id) {
        Map<String, Object> map = new HashMap<>();
        try {
            SysDept province = sysDeptMapper.findProvince(id); //查询出一个省
            Long deptId=province.getDeptId();
            InfoBaseProjectStatist infoBaseProjectStatist=infoBaseProjectStatistMapper.selectTotal();
            province.setCount(infoBaseProjectStatist.getXmmccount());
            province.setTotalcount(infoBaseProjectStatist.getTotalcount());
            province.setCencount(infoBaseProjectStatist.getCencount());
            province.setProcount(infoBaseProjectStatist.getProcount());
            province.setOthercount(infoBaseProjectStatist.getOthercount());
            List<SysDept> citys = sysDeptMapper.findChildren(deptId);//查询省下面的所有市
            children(citys);
            province.setChildren(citys);
            map.put("data", province);
            System.out.println("-----------------"+JSONObject.toJSONString(map));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    public void children(List<SysDept> citys) {
        try{
            List<SysDept> retList = new ArrayList<>();
            for (SysDept c : citys) {
                List<SysDept> sysDepts = sysDeptMapper.selectDeptss(c.getDeptId());
                List<Integer> xmNames = new ArrayList<>();
                List<BigDecimal> totalFunds = new ArrayList<>();
                List<BigDecimal> cenfunds = new ArrayList<>();
                List<BigDecimal> prfunds = new ArrayList<>();
                List<BigDecimal> othfunds = new ArrayList<>();
                for (int i = 0; i < sysDepts.size(); i++) {
                    Long id = sysDepts.get(i).getDeptId();
                    //市级信息
                    InfoBaseProjectStatist infoBaseProjectStatistCity = infoBaseProjectStatistMapper.selectInfoBaseProjectStatists(id);
                    if (infoBaseProjectStatistCity != null && infoBaseProjectStatistCity.getXmmccount() != 0) {
                        // 项目名称 总额 中央 省级 其他
                        xmNames.add(infoBaseProjectStatistCity.getXmmccount());
                        c.setXmNames(xmNames);
                        totalFunds.add(infoBaseProjectStatistCity.getTotalFunds());
                        c.setTotalFund(totalFunds);
                        cenfunds.add(infoBaseProjectStatistCity.getCentralFund());
                        c.setCentralFund(cenfunds);
                        prfunds.add(infoBaseProjectStatistCity.getProvincialFunds());
                        c.setProvincialFunds(prfunds);
                        othfunds.add(infoBaseProjectStatistCity.getOtherFund());
                        c.setOtherFund(othfunds);
                    }
                    //项目数
                    Integer xmName = 0;
                    BigDecimal totalcount = BigDecimal.ZERO;
                    BigDecimal cencount = BigDecimal.ZERO;
                    BigDecimal pc = BigDecimal.ZERO;
                    BigDecimal of = BigDecimal.ZERO;
                    for (int i2 = 0; i2 < totalFunds.size(); i2++) {
                        //资金总数
                        totalcount = totalFunds.get(i2).add(totalcount);
                        cencount = cenfunds.get(i2).add(cencount);
                        pc = prfunds.get(i2).add(pc);
                        of = othfunds.get(i2).add(of);
                        xmName += xmNames.get(i2);
                        c.setTotalcount(totalcount);
                        c.setCencount(cencount);
                        c.setProcount(pc);
                        c.setOthercount(of);
                        c.setCount(xmName);
                    }
                }
            }
            for(SysDept c:citys){
                retList = sysDeptMapper.findChildren(c.getDeptId());
                if (retList.size() > 0) {
                    for(int i=0;i<retList.size();i++){
                        //查询市级项目
                        Long di=retList.get(i).getDeptId();
                        InfoBaseProjectStatist infoBaseProjectStatist1=infoBaseProjectStatistMapper.selectInfoBaseProjectStatistListsjs(di);
                        if(infoBaseProjectStatist1!=null && infoBaseProjectStatist1.getXmmccount()!=0){
                            List<Integer> xmName=new ArrayList<>();
                            List<BigDecimal> totalFund=new ArrayList<>();
                            List<BigDecimal> cenfund=new ArrayList<>();
                            List<BigDecimal> prfund=new ArrayList<>();
                            List<BigDecimal> othfund=new ArrayList<>();
                            // 项目名称 总额 中央 省级 其他
                            xmName.add(infoBaseProjectStatist1.getXmmccount());
                            retList.get(i).setXmNames(xmName);
                            totalFund.add(infoBaseProjectStatist1.getTotalFunds());
                            retList.get(i).setTotalFund(totalFund);
                            cenfund.add(infoBaseProjectStatist1.getCentralFund());
                            retList.get(i).setCentralFund(cenfund);
                            prfund.add(infoBaseProjectStatist1.getProvincialFunds());
                            retList.get(i).setProvincialFunds(prfund);
                            othfund.add(infoBaseProjectStatist1.getOtherFund());
                            retList.get(i).setOtherFund(othfund);
                            c.setChildren(retList);
                            //项目数
                            Integer xmname1=0;
                            BigDecimal totalcount=BigDecimal.ZERO;
                            BigDecimal cencount=BigDecimal.ZERO;
                            BigDecimal pc=BigDecimal.ZERO;
                            BigDecimal of=BigDecimal.ZERO;
                            for(int i2=0;i2<totalFund.size();i2++){
                                //资金总数
                                totalcount=totalFund.get(i2).add(totalcount);
                                cencount=cenfund.get(i2).add(cencount);
                                pc=prfund.get(i2).add(pc);
                                of=othfund.get(i2).add(of);
                                xmname1+=xmName.get(i2);
                                retList.get(i).setTotalcount(totalcount);
                                retList.get(i).setCencount(cencount);
                                retList.get(i).setProcount(pc);
                                retList.get(i).setOthercount(of);
                                retList.get(i).setCount(xmname1);
                            }

                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    @Override
    public InfoBaseProjectStatist selectByid(Long id,String startTime,String lastTime) {
        List<SysDept> sysDept=sysDeptMapper.selectDeptByIdss(id);
        InfoBaseProjectStatist infoBaseProjectStatist=new InfoBaseProjectStatist();
        BigDecimal totalcount=BigDecimal.ZERO;
        BigDecimal cencount=BigDecimal.ZERO;
        BigDecimal pc=BigDecimal.ZERO;
        BigDecimal of=BigDecimal.ZERO;
        List<BigDecimal> totalFund=new ArrayList<>();
        List<BigDecimal> cenfund=new ArrayList<>();
        List<BigDecimal> prfund=new ArrayList<>();
        List<BigDecimal> othfund=new ArrayList<>();
        List<InfoBaseProjectStatist>  infoBaseProjectStatists=new ArrayList<>();
        for(int i=0;i<sysDept.size();i++){
            SysDept sysDept1=sysDept.get(i);
            Long iid=sysDept1.getDeptId();
            List<InfoBaseProjectStatist> infoBaseProjectStatistss= infoBaseProjectStatistMapper.selectByid(iid,startTime,lastTime);
            if(infoBaseProjectStatistss.size()>0){
                //金额
                for(int i1=0;i1<infoBaseProjectStatistss.size();i1++) {
                    // 总额 中央 省级 其他
                    totalFund.add(infoBaseProjectStatistss.get(i1).getTotalFunds());
                    cenfund.add(infoBaseProjectStatistss.get(i1).getCentralFund());
                    prfund.add(infoBaseProjectStatistss.get(i1).getProvincialFunds());
                    othfund.add(infoBaseProjectStatistss.get(i1).getOtherFund());
                    infoBaseProjectStatists.add(infoBaseProjectStatistss.get(i1));
                }
            }
        }
        for(int i2=0;i2<totalFund.size();i2++){
            //资金总数
            totalcount=totalFund.get(i2).add(totalcount);
            cencount=cenfund.get(i2).add(cencount);
            pc=prfund.get(i2).add(pc);
            of=othfund.get(i2).add(of);
        }
        infoBaseProjectStatist.setInfoBaseProjectStatists(infoBaseProjectStatists);
        infoBaseProjectStatist.setXmmccount(infoBaseProjectStatists.size());
        infoBaseProjectStatist.setTotalcount(totalcount);
        infoBaseProjectStatist.setCencount(cencount);
        infoBaseProjectStatist.setProcount(pc);
        infoBaseProjectStatist.setOthercount(of);
        return infoBaseProjectStatist;
    }

    @Override
    public List<Map<String, Object>> getRegionProject(Long id,String startTime,String lastTime){
        if(ObjectUtil.isNotEmpty(startTime)&&ObjectUtil.isNotEmpty(lastTime)){
            return infoBaseProjectStatistMapper.selectRegionProject(id,startTime,lastTime);
        }else{
            return infoBaseProjectStatistMapper.selectRegionProject(id,null,null);
        }

    }
}
